﻿using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using LFloatMath.Math;
using NetProtocol.POD;

namespace IQIGame.Onigao.Logic
{
    public class BattlePermitCastingState : BattleBaseSkillState
    {
        public BattlePermitCastingState(BattlePlayController playController) :
            base(playController)
        {
        }

        public override EBattleSkillState stateId => EBattleSkillState.PermitCast;

        public override void OnInit()
        {
        }

        public override void OnEnter(EBattleSkillState prevStateId)
        {
            var rCurTurn = this.m_PlayCtrl.queueCtrl.current;
            var rCurSkill = rCurTurn.curSkill;
            // 本turn中上一级能结束时可能会指定当前要释放的技能
            if (rCurSkill != null)
            {
                this.ChangeState(EBattleSkillState.SkillExecute);
            }
            else
            {
                var rAIComp = rCurTurn.unit.GetComponent<AIComponent>(ETComponentType.AIComponet);
                if (this.m_PlayCtrl.skiping || (rAIComp?.active ?? false))
                {
                    var rAIMovePath = ListPool<LVector2Int>.Get();
                    var rAIInput = rAIComp.GetAIInput(ref rAIMovePath);
                    this.m_PlayCtrl.CastSkill(rAIInput, rAIMovePath);
                    ListPool<LVector2Int>.Put(rAIMovePath);
                }
                else
                {
                    this.m_PlayCtrl.battle.NotifyPermitCast(rCurTurn.unit.GUID, rCurSkill?.skillID ?? 0);
                }
            }
        }

        public override void OnExit()
        {
        }

        public override void Dispose()
        {
        }
    }
}